const { msg: msgController } = require('../controller/')

class WebSockets {
  users = []
  connection(client) {
    console.log('a user connected')

    client.on('chatMsg', async ({ msg, senderId, receiverId }) => {
      await msgController.createMsg({
        msg,
        senderId,
        receiverId,
      })

      global.io.emit(receiverId, { msg, senderId })
    })
  }
}

module.exports = new WebSockets()
